package test.com.newproject.moudle.main;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import butterknife.BindView;
import library.com.httphelper.HttpHelper;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
import test.com.newproject.R;
import test.com.newproject.framework.ui.BaseActivity;
import test.com.newproject.moudle.main.discover.DiscoverFragment;
import test.com.newproject.moudle.main.homepage.HomepageFragment;
import test.com.newproject.moudle.main.mine.MineFragment;
import test.com.newproject.tool.helper.UUIDHelper;
import test.com.newproject.tool.helper.UpdateHelper;

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.activity_main_bnv)
    protected BottomNavigationView bnv;

    protected FragmentManager fragmentManager;
    protected HomepageFragment homepageFragment;
    protected DiscoverFragment discoverFragment;
    protected MineFragment mineFragment;
    private String TAG = "debug";
    private static final int CODE_PERMISSION = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //UpdateHelper.INSTANCE.checkVersion(this);
        requestPermission();
    }

    @Override
    protected void initView() {
        super.initView();
        fragmentManager = getSupportFragmentManager();
        homepageFragment = new HomepageFragment();
        discoverFragment = new DiscoverFragment();
        mineFragment = new MineFragment();
        bnv.setOnNavigationItemSelectedListener(this);
        bnv.setSelectedItemId(R.id.homepage);
    }

    @Override
    protected int obtainHomeIcon() {
        return R.drawable.ic_back;
    }

    @Override
    protected String obtainTitle() {
        return "主页";
    }

    @Override
    protected boolean titleCenter() {
        return true;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("set").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return true;
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        Log.d(TAG, "onBackPressed: ");
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        switch (item.getItemId()) {
            case R.id.homepage:
                setTitle("首页");
                if (homepageFragment.isVisible()) {
                    return true;
                }
                fragmentTransaction.hide(discoverFragment);
                fragmentTransaction.hide(mineFragment);

                if (!homepageFragment.isAdded()) {
                    fragmentTransaction.add(R.id.activity_main_content_fl, homepageFragment);
                }

                fragmentTransaction.show(homepageFragment);

                break;
            case R.id.discover:
                setTitle("发现");
                if (discoverFragment.isVisible()) {
                    return true;
                }
                fragmentTransaction.hide(homepageFragment);
                fragmentTransaction.hide(mineFragment);

                if (!discoverFragment.isAdded()) {
                    fragmentTransaction.add(R.id.activity_main_content_fl, discoverFragment);
                }

                fragmentTransaction.show(discoverFragment);
                break;
            case R.id.mine:
                setTitle("我的");
                if (mineFragment.isVisible()) {
                    return true;
                }
                fragmentTransaction.hide(homepageFragment);
                fragmentTransaction.hide(discoverFragment);

                if (!mineFragment.isAdded()) {
                    fragmentTransaction.add(R.id.activity_main_content_fl, mineFragment);
                }

                fragmentTransaction.show(mineFragment);
                break;
        }

        fragmentTransaction.commit();

        return true;
    }

    @AfterPermissionGranted(CODE_PERMISSION)
    protected void requestPermission() {
        if (EasyPermissions.hasPermissions(this, Manifest.permission.READ_PHONE_STATE)) {
            UUIDHelper.INSTANCE.createUUID(this);
            return;
        }

        EasyPermissions.requestPermissions(this, "", CODE_PERMISSION, Manifest.permission.READ_PHONE_STATE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
}
